package com.example.exchangeapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.Manifest;
import android.content.pm.PackageManager;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;

import android.Manifest;
import android.content.pm.PackageManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import android.view.MenuItem;
import android.widget.FrameLayout;

import com.example.exchangeapp.yy.constant.UserLogin;
import com.example.exchangeapp.yy.entity.GoodVo2;
import com.example.exchangeapp.yy.fragment.HasSendGoods;
import com.example.exchangeapp.yy.fragment.IndexFragment;
import com.example.exchangeapp.yy.fragment.MyFragment;
import com.example.exchangeapp.yy.fragment.SendFragment;
import com.example.exchangeapp.yy.fragment.class_Fragment;
import com.example.exchangeapp.yy.request.HttpRequestUtils;
import com.example.exchangeapp.zjs.GetUserStatus;
import com.example.exchangeapp.zjs.login;
import com.google.android.material.bottomnavigation.BottomNavigationView;



import android.widget.Toast;




import android.widget.Toast;



public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private FrameLayout frameLayout;
    private IndexFragment indexFragment;
    private SendFragment sendFragment;
    private MyFragment myFragment;
    private static final int REQUEST_PERMISSION_READ_EXTERNAL_STORAGE = 1001;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //zjs--判断数据库是否存在用户,存在则显示该页面，否则跳转到登录界面
        GetUserStatus getUserStatus = new GetUserStatus(MainActivity.this);
        int count = getUserStatus.getDataFromDatabase().getCount();
        if (count == 0) {
            // 跳转到登录界面
            startActivity(new Intent(this, login.class));
        }else {
            yyinit();
        }

        //zjs------获取从 login 传递过来的 extras
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            // 显示登录成功的提示信息
            String loginMessage = extras.getString("login_message");
            Toast.makeText(this, loginMessage, Toast.LENGTH_SHORT).show();

            String loginCode = extras.getString("login_code");
            if(loginCode.equals("200")){
                yyinit();
            }
        }


        pxxinit();
    }

    private void pxxinit() {
        // 检查是否已经获取了读取外部存储器的权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            // 如果没有获取权限，则请求权限
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQUEST_PERMISSION_READ_EXTERNAL_STORAGE);
        } else {
            // 如果已经获取了权限，则可以读取图片文件
            // ...
        }
    }

    private void yyinit() {

        indexFragment = new IndexFragment();
        myFragment = new MyFragment();
        sendFragment = new SendFragment();
        GetUserStatus getUserStatus = new GetUserStatus(MainActivity.this);
        Cursor cursor = getUserStatus.getDataFromDatabase();
        int id = 0;
        while (cursor.moveToNext()){
            int index = cursor.getColumnIndex("ID");
            id = cursor.getInt(index);
        }
        //登录的用户id
        UserLogin.uid=id;

        frameLayout = findViewById(R.id.home_fragment);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(itemSelectedListener);

        //主页显示indexFramework
        switchFragment(indexFragment);
        //请求到所有商品的类型和对应的id
        HttpRequestUtils.get();

        //HttpRequestUtils.get();
//        switchFragment(indexFragment);
//        switchFragment(myFragment);
       HttpRequestUtils.getHasSendGoodsNum(UserLogin.uid+"", 1, 10);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(HasSendGoods.num);
       HttpRequestUtils.getHasSendGoods(UserLogin.uid+"", 1, HasSendGoods.num);


        //请求到所有商品的类型和对应的id
      //  HttpRequestUtils.get();


//        switchFragment(indexFragment);
//        switchFragment(myFragment);


        //给点击商品详情返回按钮绑定点击时间

    }

    private BottomNavigationView.OnNavigationItemSelectedListener itemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.index:
                    switchFragment(indexFragment);
                    return true;
                case R.id.send:
                    switchFragment(sendFragment);
                    return true;
                case R.id.my:
                    switchFragment(myFragment);
                    return true;
            }
            return false;
        }
    };

    private void switchFragment(Fragment fragment){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.home_fragment, fragment).commitNow();
    }

    // 处理权限请求的结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_READ_EXTERNAL_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限请求成功，可以读取图片文件
                // ...
            } else {
                // 权限请求失败，无法读取图片文件
                // ...
            }
        }
    }

}